package io.wdb.modules.generator.generator.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.wdb.modules.generator.generator.dao.BookgridInfoDao;
import io.wdb.modules.generator.generator.dao.UserMaintainLogDao;
import io.wdb.modules.generator.generator.entity.BookgridInfoEntity;
import io.wdb.modules.generator.generator.entity.UserMaintainLogEntity;
import io.wdb.modules.generator.generator.entity.statistics.BookGridAvailableRes;
import io.wdb.modules.generator.generator.entity.web.BookShelfOperation;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.BookgridInfoService;
import io.wdb.modules.manager.sys.entity.SysUserEntity;



@Service("bookgridInfoService")
public class BookgridInfoServiceImpl implements BookgridInfoService {
	@Autowired
	private BookgridInfoDao bookgridInfoDao;
	
	@Autowired
	private UserMaintainLogDao userMaintainLogDao;
	@Override
	public BookgridInfoEntity queryObject(String bookgridCode){
		return bookgridInfoDao.queryObject(bookgridCode);
	}
	
	@Override
	public List<BookgridInfoEntity> queryList(Map<String, Object> map){
		return bookgridInfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return bookgridInfoDao.queryTotal(map);
	}
	
	@Override
	public void save(BookgridInfoEntity bookgridInfo){
		bookgridInfoDao.save(bookgridInfo);
	}
	
	@Override
	public void update(BookgridInfoEntity bookgridInfo){
		
		bookgridInfoDao.update(bookgridInfo);
	}
	
	@Override
	public void delete(String bookgridCode){
		bookgridInfoDao.delete(bookgridCode);
	}
	
	@Override
	public void deleteBatch(String[] bookgridCodes){
		bookgridInfoDao.deleteBatch(bookgridCodes);
	}

	@Override
	public WebResponse updateAvailable(BookShelfOperation bookShelfOperation) {
		WebResponse response = new WebResponse();
		BookgridInfoEntity bookgridInfo = bookgridInfoDao.queryObject(bookShelfOperation.getBookGridCode());
		if(bookgridInfo==null) {
			response.setCode(400);
			response.setMessage("未能查询到相应书格信息");
			return response;
		}
		if(bookgridInfo.getAvailable()==0) {//已经不可用
			UserMaintainLogEntity userMaintainLogEntity = userMaintainLogDao.queryByGridMax(bookShelfOperation.getBookGridCode());
			if(userMaintainLogEntity==null) {
				userMaintainLogEntity = new UserMaintainLogEntity();
				userMaintainLogEntity.setMaintainBatch(UUID.randomUUID().toString().replaceAll("-", ""));
				userMaintainLogEntity.setBookgridCode(bookShelfOperation.getBookGridCode());
			}
			userMaintainLogEntity.setCreateTime(new Date());
			userMaintainLogEntity.setDetail(bookShelfOperation.getExplain());
			userMaintainLogEntity.setMaintainType("1");
			userMaintainLogEntity.setMaintainValue(bookShelfOperation.getAvailable());
			SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
			userMaintainLogEntity.setUserId(sysUserEntity==null?null:sysUserEntity.getUserId());
			userMaintainLogDao.save(userMaintainLogEntity);
		}else {//当前可用
			UserMaintainLogEntity userMaintainLogEntity = new UserMaintainLogEntity();
			userMaintainLogEntity.setBookgridCode(bookShelfOperation.getBookGridCode());
			userMaintainLogEntity.setCreateTime(new Date());
			userMaintainLogEntity.setDetail(bookShelfOperation.getExplain());
			userMaintainLogEntity.setMaintainType("1");
			userMaintainLogEntity.setMaintainValue(bookShelfOperation.getAvailable());
			userMaintainLogEntity.setMaintainBatch(UUID.randomUUID().toString().replaceAll("-", ""));
			SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
			userMaintainLogEntity.setUserId(sysUserEntity==null?null:sysUserEntity.getUserId());
			userMaintainLogDao.save(userMaintainLogEntity);
		}
		bookgridInfo.setAvailable(Integer.valueOf(bookShelfOperation.getAvailable()));
		bookgridInfoDao.update(bookgridInfo);
		response.setCode(100);
		response.setMessage("书格是否可用状态设置成功");
		return response;
	}

	@Override
	public List<BookGridAvailableRes> getGridAvailableDetail(BookShelfOperation bookShelfOperation) {
		List<BookGridAvailableRes> list = new ArrayList<>();
		List<BookgridInfoEntity> bookgridInfoEntities = bookgridInfoDao.queryGridCountByShelfCode(bookShelfOperation.getBookShelfCode());
		for(BookgridInfoEntity bookgridInfoEntity:bookgridInfoEntities) {
			BookGridAvailableRes bookGridAvailableRes = new BookGridAvailableRes();
			if(bookgridInfoEntity.getAvailable()==1) {
				bookGridAvailableRes.setBookGridCode(bookgridInfoEntity.getBookgridCode());
				bookGridAvailableRes.setStatus("1");
				bookGridAvailableRes.setDetail("");
			}else {
				bookGridAvailableRes.setBookGridCode(bookgridInfoEntity.getBookgridCode());
				bookGridAvailableRes.setStatus("0");
				UserMaintainLogEntity entity = userMaintainLogDao.queryByGridMax(bookgridInfoEntity.getBookgridCode());
				if(entity!=null) {
					List<UserMaintainLogEntity> userMaintainLogEntities = userMaintainLogDao.queryByMaintain(entity.getMaintainBatch());
					StringBuffer buffer = new StringBuffer();
					if(userMaintainLogEntities!=null) {
						for(UserMaintainLogEntity userMaintainLogEntity:userMaintainLogEntities) {
							if(userMaintainLogEntity.getDetail()==null||userMaintainLogEntity.getDetail().equals("")) {
								continue;
							}
							if(buffer.length()>0) {
								buffer.append(";");
							}
							if(userMaintainLogEntity.getDetail()!=null&&!userMaintainLogEntity.getDetail().equals("")) {
								buffer.append(userMaintainLogEntity.getDetail());
							}else {
								buffer.append("");
							}
						}
					}
					bookGridAvailableRes.setDetail(buffer.toString());
				}
			}
			list.add(bookGridAvailableRes);
		}
		return list;
	}
	
}
